package days4.easy.title69;

/**
 * @Author fengzihao
 * @Date 2024-04-18 21:25
 */
class Solution {
    public static void main(String[] args) {
        int a = mySqrt(2147483647);
        System.out.println("a = " + a);
    }
    public static int countDigits(int number) {
        if (number < 0) {
            number = -number; // 如果是负数，转换为正数
        }
        int digits = 0;
        while (number > 0) {
            digits++;
            number /= 10;
        }
        return digits;
    }
    public static int mySqrt(int x) {
        int digits = countDigits(x);
        if (digits > 2) {
            int start = 1;
            for (int i = 0; i < digits / 2 - 1; i++) {
                start *= 10;
            }
            int i = start;
            for ( ;; i++) {
                long temp = (long) i *i;
                if (temp > x){
                    break;
                }
            }
            return i - 1;
        } else {
            for (int i = 0; ; i++) {
                if (i * i > x)
                    return i - 1;
            }
        }
    }
}
